#include "all.h"

// JSON 解析函数
int parse_upjson_to_struct(const char *json_str, struct CJSON *data)
{
    cJSON *root = cJSON_Parse(json_str);
    if (!root)
    {
        printf("Error parsing JSON: %s\n", cJSON_GetErrorPtr());
        return -1;
    }

    // 解析版本信息
    cJSON *version = cJSON_GetObjectItem(root, "version");
    if (version && cJSON_IsString(version))
    {
        strncpy(data->version, version->valuestring, sizeof(data->version) - 1);
    }
 
    // 解析报告
    cJSON *report = cJSON_GetObjectItem(root, "report");
    if (report)
    {
        cJSON *type = cJSON_GetObjectItem(report, "type");
        cJSON *period = cJSON_GetObjectItem(report, "period");
        if (type && cJSON_IsNumber(type))
            data->report_type = type->valueint;
        if (period && cJSON_IsNumber(period))
            data->report_period = period->valueint;
    }

    // 解析 MQTT 服务器信息
    cJSON *mqtt_server = cJSON_GetObjectItem(root, "mqtt_server");
    if (mqtt_server)
    {
        cJSON *addr = cJSON_GetObjectItem(mqtt_server, "addr");
        cJSON *port = cJSON_GetObjectItem(mqtt_server, "port");
        if (addr && cJSON_IsString(addr))
            strncpy(data->mqtt_server_addr, addr->valuestring, sizeof(data->mqtt_server_addr) - 1);
        if (port && cJSON_IsNumber(port))
            data->mqtt_server_port = port->valueint;
    }

    // 解析 Modbus 设备信息
    cJSON *mb_dev = cJSON_GetObjectItem(root, "mb_dev");
    if (mb_dev)
    {
        cJSON *addr = cJSON_GetObjectItem(mb_dev, "addr");
        cJSON *port = cJSON_GetObjectItem(mb_dev, "port");
        if (addr && cJSON_IsString(addr))
            strncpy(data->mb_dev_addr, addr->valuestring, sizeof(data->mb_dev_addr) - 1);
        if (port && cJSON_IsNumber(port))
            data->mb_dev_port = port->valueint;
    }

    // 解析 STM32 数据
    cJSON *stm32 = cJSON_GetObjectItem(root, "stm32");
    if (stm32)
    {
        cJSON *stm32_data = cJSON_GetObjectItem(stm32, "data");
        int i = 0;
        cJSON *item;
        cJSON_ArrayForEach(item, stm32_data)
        {
            if (i >= 14)
                break;
            data->stm32.data[i].data_key = cJSON_GetObjectItem(item, "key")->valueint;
            strncpy(data->stm32.data[i].data_name, cJSON_GetObjectItem(item, "name")->valuestring, sizeof(data->stm32.data[i].data_name) - 1);
            data->stm32.data[i].data_type = cJSON_GetObjectItem(item, "type")->valueint;

            if (data->stm32.data[i].data_type == 1 || data->stm32.data[i].data_type == 2)
            {
                data->stm32.data[i].val.data_val_i = cJSON_GetObjectItem(item, "val")->valueint;
            }
            else if (data->stm32.data[i].data_type == 3)
            {
                data->stm32.data[i].val.data_val_f = cJSON_GetObjectItem(item, "val")->valuedouble;
            }
            i++;
        }
    }

    // 解析 Modbus 数据
    cJSON *modbus = cJSON_GetObjectItem(root, "modbus");
    if (modbus)
    {
        cJSON *modbus_data = cJSON_GetObjectItem(modbus, "data");
        int i = 0;
        cJSON *item;
        cJSON_ArrayForEach(item, modbus_data)
        {
            if (i >= 6)
                break;
            data->modbus.data[i].data_key = cJSON_GetObjectItem(item, "key")->valueint;
            strncpy(data->modbus.data[i].data_name, cJSON_GetObjectItem(item, "name")->valuestring, sizeof(data->modbus.data[i].data_name) - 1);
            data->modbus.data[i].data_name[sizeof(data->modbus.data[i].data_name) - 1] = '\0'; // 确保 '\0' 结尾

            // 修复 data_addr 的解析
            cJSON *addr = cJSON_GetObjectItem(item, "addr");
            if (addr && cJSON_IsString(addr))
            {
                strncpy(data->modbus.data[i].data_addr, addr->valuestring, sizeof(data->modbus.data[i].data_addr) - 1);
                data->modbus.data[i].data_addr[sizeof(data->modbus.data[i].data_addr) - 1] = '\0'; // 确保 '\0' 结尾
            }

            data->modbus.data[i].data_type = cJSON_GetObjectItem(item, "type")->valueint;

            if (data->modbus.data[i].data_type == 1 || data->modbus.data[i].data_type == 2)
            {
                data->modbus.data[i].val.data_val_i = cJSON_GetObjectItem(item, "val")->valueint;
            }
            else if (data->modbus.data[i].data_type == 3)
            {
                data->modbus.data[i].val.data_val_f = cJSON_GetObjectItem(item, "val")->valuedouble;
            }
            i++;
        }
    }

    cJSON_Delete(root);
    return 0;
}

int parse_downjson_to_struct(const char *json_str, struct msgQueue *queue)
{
    // 解析 JSON 字符串
    cJSON *root = cJSON_Parse(json_str);
    if (root == NULL)
    {
        printf("Error parsing JSON\n");
        return -1;
    }

    // 提取 key
    cJSON *key = cJSON_GetObjectItem(root, "key");
    if (key && cJSON_IsNumber(key))
    {
        queue->key = key->valuedouble;
    }
    else
    {
        printf("Missing or invalid key\n");
        cJSON_Delete(root);
        return -1;
    }

    // 提取 data_Humidifier
    cJSON *data_Humidifier = cJSON_GetObjectItem(root, "data_Humidifier");
    queue->text.data_Humidifier = data_Humidifier && cJSON_IsNumber(data_Humidifier) ? data_Humidifier->valueint : 0;

    // 提取 data_Dehumidifier
    cJSON *data_Dehumidifier = cJSON_GetObjectItem(root, "data_Dehumidifier");
    queue->text.data_Dehumidifier = data_Dehumidifier && cJSON_IsNumber(data_Dehumidifier) ? data_Dehumidifier->valueint : 0;

    // 提取 data_TemController
    cJSON *data_TemController = cJSON_GetObjectItem(root, "data_TemController");
    queue->text.data_TemController = data_TemController && cJSON_IsNumber(data_TemController) ? data_TemController->valueint : 0;

    // 提取 Tmp_Hign
    cJSON *Tmp_Hign = cJSON_GetObjectItem(root, "Tmp_Hign");
    queue->text.Tmp_Hign = Tmp_Hign && cJSON_IsNumber(Tmp_Hign) ? Tmp_Hign->valueint : 0;

    // 提取 Tmp_Low
    cJSON *Tmp_Low = cJSON_GetObjectItem(root, "Tmp_Low");
    queue->text.Tmp_Low = Tmp_Low && cJSON_IsNumber(Tmp_Low) ? Tmp_Low->valueint : 0;

    // 提取 Hum
    cJSON *Hum = cJSON_GetObjectItem(root, "Hum");
    queue->text.Hum = Hum && cJSON_IsNumber(Hum) ? Hum->valueint : 0;

    // 提取 mode
    cJSON *mode = cJSON_GetObjectItem(root, "mode");
    queue->text.mode = mode && cJSON_IsNumber(mode) ? mode->valueint : 0;

    // 提取 mode_aircondition
    cJSON *mode_aircondition = cJSON_GetObjectItem(root, "mode_aircondition");
    queue->text.mode_aircondition = mode_aircondition && cJSON_IsNumber(mode_aircondition) ? mode_aircondition->valueint : FALSE;

    // 提取 state_Humidifier
    cJSON *state_Humidifier = cJSON_GetObjectItem(root, "state_Humidifier");
    queue->text.state_Humidifier = state_Humidifier && cJSON_IsNumber(state_Humidifier) ? state_Humidifier->valueint : FALSE;

    // 提取 state_Dehumidifier
    cJSON *state_Dehumidifier = cJSON_GetObjectItem(root, "state_Dehumidifier");
    queue->text.state_Dehumidifier = state_Dehumidifier && cJSON_IsNumber(state_Dehumidifier) ? state_Dehumidifier->valueint : FALSE;

    // 提取 state_TemController
    cJSON *state_TemController = cJSON_GetObjectItem(root, "state_TemController");
    queue->text.state_TemController = state_TemController && cJSON_IsNumber(state_TemController) ? state_TemController->valueint : FALSE;

    // 清理
    cJSON_Delete(root);
    return 0;
}


int upjson_to_struct(const char *json_str, struct CJSON *data)
{
    cJSON *root = cJSON_Parse(json_str);
    if (!root)
    {
        printf("Error parsing JSON: %s\n", cJSON_GetErrorPtr());
        return -1;
    } 
    // 解析报告
    cJSON *report = cJSON_GetObjectItem(root, "report");
    if (report)
    {
        cJSON *type = cJSON_GetObjectItem(report, "type");
        cJSON *period = cJSON_GetObjectItem(report, "period");
        if (type && cJSON_IsNumber(type))
            data->report_type = type->valueint;
        if (period && cJSON_IsNumber(period))
            data->report_period = period->valueint;
    }
    return 0;
}
